﻿using FitnessCenter.Common.Constants;
using FitnessCenter.WpfApp.Events;
using Microsoft.Practices.Prism.PubSubEvents;
using System.Windows;

namespace FitnessCenter.WpfApp.Views
{
    /// <summary>
    /// Interaction logic for Shell.xaml
    /// </summary>
    public partial class Shell : Window
    {
        private readonly IEventAggregator _eventAggregator;
        private string ActiveModuleName { get; set; }

        public Shell(IEventAggregator eventAggregator)
        {
            InitializeComponent();

            _eventAggregator = eventAggregator;
            SubscribeToEvents();

            ActiveModuleName = ModuleNames.DashboardModule;
            SetModulesVisibilities();
        }

        #region Methods

        private void SubscribeToEvents()
        {
            _eventAggregator.GetEvent<SelectModuleEvent>().Subscribe(OnSelectModule);
        }

        private void OnSelectModule(string moduleName)
        {
            var activeModule = GetActiveModule();
            SetVisibility(activeModule, Visibility.Collapsed);

            var module = LogicalTreeHelper.FindLogicalNode(this, moduleName);
            SetVisibility(module, Visibility.Visible);
            ActiveModuleName = moduleName;
        }

        private void SetModulesVisibilities()
        {
            var module = LogicalTreeHelper.FindLogicalNode(this, ActiveModuleName);
            SetVisibility(module, Visibility.Visible);

            module = LogicalTreeHelper.FindLogicalNode(this, ModuleNames.PersonModule);
            SetVisibility(module, Visibility.Collapsed);

            module = LogicalTreeHelper.FindLogicalNode(this, ModuleNames.LogActivityModule);
            SetVisibility(module, Visibility.Collapsed);

            module = LogicalTreeHelper.FindLogicalNode(this, ModuleNames.ActivityModule);
            SetVisibility(module, Visibility.Collapsed);
        }

        private void SetVisibility(DependencyObject obj, Visibility value)
        {
            obj.SetValue(VisibilityProperty, value);
        }

        private DependencyObject GetActiveModule()
        {
            return LogicalTreeHelper.FindLogicalNode(this, ActiveModuleName); ;
        }
        #endregion
    }
}
